﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace FinalSettlementModel
{
    public class User : Helper
    {
        public int userIdentity { set; get; }

        // Tài khoản dùng chung
        public int ID {set; get;}
        public string Fullname { set; get; }
        public string Username { set; get; }
        public bool Master { set; get; }
        public bool Lock { set; get; }
        public string Address { set; get; }
        public string Phone { set; get; }
        public string Email { set; get; }

        // Quyền sử dụng
        public bool TiepNhanHoSo {set; get;}
        public bool GiaoHoSo {set; get;}
        public bool DuLieuHeThong {set; get;}
        public bool NguoiDung {set; get;}
        public bool SaoLuu {set; get;}
        public bool PhucHoi {set; get;}
        public bool GiaoDien {set; get;}
        public bool TrinhPheDuyet {set; get;}
        public bool PheDuyetQT {set; get;}
        public bool BCTongHop {set; get;}
        public bool BCHoSoMoi {set; get;}
        public bool BCTinhHinhQT {set; get;}
        public bool BCTinhHinhHSChuaQT {set; get;}
        public bool BCChiTietHSChuaQT {set; get;}
        public bool BCChiTietHSDaQT { set; get; }
                
        public bool CheckUserLogin(string strUsername, string strPassword)
        {

            string query =      "SELECT U.*, " +
                                "Q.[TiepNhanHoSo], " +
                                "Q.[GiaoHoSo], " +
                                "Q.[DuLieuHeThong], " +
                                "Q.[NguoiDung], " +
                                "Q.[SaoLuu], " +
                                "Q.[PhucHoi], " +
                                "Q.[GiaoDien], " +
                                "Q.[TrinhPheDuyet], " +
                                "Q.[PheDuyetQT], " +
                                "Q.[BCTongHop], " +
                                "Q.[BCHoSoMoi], " +
                                "Q.[BCTinhHinhQT], " +
                                "Q.[BCTinhHinhHSChuaQT], " +
                                "Q.[BCChiTietHSChuaQT], " +
                                "Q.[BCChiTietHSDaQT] " +

                                "FROM dbo.[TblUser] AS U  " +
                                "LEFT JOIN dbo.[TblPhanQuyen] AS Q ON Q.NguoiDungId = U.ID  " +
                                "WHERE U.Username=@UserName AND U.Password=@Password";

                SqlParameter spU = new SqlParameter("@UserName", strUsername);
                SqlParameter spP = new SqlParameter("@Password", strPassword);

                DataTable dt = this.ExecuteDataTable(query, new SqlParameter[] { spU, spP });

                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        // Lưu dữ liệu người dùng vào biến để dùng chung
                        DataRow row = dt.Rows[0];
                        ID = (int)row["ID"];
                        Fullname = (string)row["Fullname"];
                        Username = (string)row["Username"];
                        Master = (bool)row["Master"];
                        Lock = (bool)row["Lock"];
                        Address = row["Address"].ToString();
                        Phone = row["Phone"].ToString();
                        Email = row["Email"].ToString();

                        //Lưu quyền
                        TiepNhanHoSo = (bool)row["TiepNhanHoSo"];
                        GiaoHoSo = (bool)row["GiaoHoSo"];
                        DuLieuHeThong = (bool)row["DuLieuHeThong"];
                        NguoiDung = (bool)row["NguoiDung"];
                        SaoLuu = (bool)row["SaoLuu"];
                        PhucHoi = (bool)row["PhucHoi"];
                        GiaoDien = (bool)row["GiaoDien"];
                        TrinhPheDuyet = (bool)row["TrinhPheDuyet"];
                        PheDuyetQT = (bool)row["PheDuyetQT"];
                        BCTongHop = (bool)row["BCTongHop"];
                        BCHoSoMoi = (bool)row["BCHoSoMoi"];
                        BCTinhHinhQT = (bool)row["BCTinhHinhQT"];
                        BCTinhHinhHSChuaQT = (bool)row["BCTinhHinhHSChuaQT"];
                        BCChiTietHSChuaQT = (bool)row["BCChiTietHSChuaQT"];
                        BCChiTietHSDaQT = (bool)row["BCChiTietHSDaQT"];

                        return true;

                    }
                    catch (Exception ex)
                    {
                        Message = ex.Message;
                        return false;
                    }
                }
                return false;            
        }

        public DataTable ListUserData()
        {
            string query = "SELECT ID,Fullname,Username,Master,Lock,Address,Phone,Email FROM dbo.[TblUser]";

            return this.ExecuteDataTable(query, null); 
        }

        // Hàm kiểm tra dữ liệu người dùng đã tồn tại hay chưa (sử dụng cho phương thức tạo mới và cập nhật)
        public bool checkUserExist(string userName)
        {
            var checkUser = from check in nc.TblUsers
                            where check.Username == userName
                            select check;
            if (checkUser.Any())
            {
                return true;
            }
            return false;
        }

        // Hàm tạo mới dữ liệu người dùng
        public void insertUser(string fullName, string userName, string passWord, string address, string phone, string email, bool master, bool locked)
        {
            TblUser user = new TblUser();
            user.Fullname = fullName;
            user.Username = userName;
            user.Password = passWord;
            user.Address = address;
            user.Phone = phone;
            user.Email = email;
            user.Master = master;
            user.Lock = locked;
            nc.TblUsers.InsertOnSubmit(user);
            nc.SubmitChanges();

            userIdentity = user.ID;
        }
        
        // Hàm cập nhật dữ liệu người dùng
        public void updateUser(int id, string fullName, string userName, string passWord, string address, string phone, string email, bool master, bool locked)
        {
            var user = nc.TblUsers.Single(s => s.ID == id);
            user.Fullname = fullName;
            user.Username = userName;
            if (passWord != "")
            {
                user.Password = NHTU.Hash.Md5(passWord);
            }
            user.Address = address;
            user.Phone = phone;
            user.Email = email;
            user.Master = master;
            user.Lock = locked;
            nc.SubmitChanges();
        }

        // Hàm xóa dữ liệu người dùng
        public void deleteUser(int id)
        {
            var user = nc.TblUsers.Single(s => s.ID == id);
            nc.TblUsers.DeleteOnSubmit(user);
            nc.SubmitChanges();
        }

        // Hàm khóa / mở khóa tài khoản
        public void lockUser(int id, bool islock)
        {
            var _u = nc.TblUsers.Single(u => u.ID == id);
            if (_u != null)
            {
                _u.Lock = islock;
                nc.SubmitChanges();
            }
        }

        // Hàm kiểm tra và thay đổi mật khẩu người dùng
        public bool checkAndChangePassword(int id, string username, string oldPass, string newPass)
        {
            TblUser user = nc.TblUsers.Single(s => s.ID == id && s.Username == username);
            if (user.Password != NHTU.Hash.Md5(oldPass))
            {
                return false;
            }
            else
            {
                user.Password = NHTU.Hash.Md5(newPass);
                nc.SubmitChanges();
                return true;
            }
        }
    }
}
